#include "Blocks/PostedSignBlock.h"
#include "Blocks/AttachedSignBlock.h"
#include "Components/ComponentMiner.h"
#include "Components/ComponentCreature.h"
#include "Components/ComponentCreatureModel.h"
#include <cfloat>

namespace PocketSurvival
{
    PostedSignBlock::PostedSignBlock(int32_t attachedSign) : attachedSignBlockIndex(attachedSign)
    {
        addInheritance(this);
        addInheritance((IPaintableBlock*)this);
    }

    void PostedSignBlock::getCreativeValues(std::vector<int32_t> &blockVec)
    {
        blockVec.push_back(Terrain::MakeBlockValue(getIndex(), 0, SetColor(0, -1)));
        for(int32_t index = 0; index < 16; ++index)
        {
            blockVec.push_back(Terrain::MakeBlockValue(getIndex(), 0, SetColor(0, index)));
        }
    }

    void PostedSignBlock::getDropValues(int32_t oldValue, int32_t newValue, int32_t toolLevel, std::vector<BlockDropValue> &dropValues)
    {
		int32_t data = SetVariant(Terrain::ExtractData(oldValue), 0);
        dropValues.push_back(BlockDropValue { Terrain::MakeBlockValue(getIndex(), 0, data), 1 });
    }

    BlockPlacementData PostedSignBlock::getPlacementValue(ComponentMiner *componentMiner, int32_t value, const TerrainRaycastResult &raycastResult)
    {
		int32_t color = GetColor(Terrain::ExtractData(value));
		BlockPlacementData result;
		if (raycastResult.cellFace.face < 4)
		{
            // 附着的告示牌
			int32_t data = AttachedSignBlock::SetFace(AttachedSignBlock::SetColor(0, color), raycastResult.cellFace.face);
			result.value = Terrain::MakeBlockValue(attachedSignBlockIndex, 0, data);
			result.cellFace = raycastResult.cellFace;
			return result;
		}
		Vector3 forward = componentMiner->componentCreature->componentCreatureModel->getEyeRotation().toMatrix().getForward();
		float num = FLT_MIN;
		int32_t direction = 0;
		for (int32_t i = 0; i < 8; i++)
		{
			float num2 = Vector3::Dot(forward, BlockConstans::postedSignDirections[i]);
			if (num2 > num)
			{
				num = num2;
				direction = i;
			}
		}
		int32_t data2 = SetHanging(SetDirection(SetColor(0, color), direction), raycastResult.cellFace.face == 5);
		result.value = Terrain::MakeBlockValue(getIndex(), 0, data2);
		result.cellFace = raycastResult.cellFace;
		return result;
    }

    int32_t PostedSignBlock::getPaintColor(int32_t value)
    {
        return GetColor(Terrain::ExtractData(value));
    }

    int32_t PostedSignBlock::paint(int32_t value, int32_t color)
    {
        return Terrain::MakeBlockValue(getIndex(), 0, SetColor(Terrain::ExtractData(value), color));
    }



} // namespace PocketSurvival
